//
//  NSString+Plugins.m
//  MobileOA
//
//  Created by Woo Jack on 13-5-15.
//  Copyright (c) 2013年 chenxiaojuan. All rights reserved.
//

#import "Class+Plugins.h"
#import <CommonCrypto/CommonDigest.h>


@implementation NSString (Plugins)

- (NSDate *)dateFromString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyyMMddHHmmssSSS"];
    NSDate *destDate= [dateFormatter dateFromString:self];
    [dateFormatter release];
    return destDate;
}

- (NSString*)md5Hash
{
    NSData* data = [self dataUsingEncoding:NSUTF8StringEncoding];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5([data bytes], [data length], result);
    
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
            ];
}

-(BOOL)containString:(NSString*)string{
    
    NSRange range = [self rangeOfString:string];
    if (range.length > 0)
    {
        return YES;
    }
    return NO;
}

@end

@implementation NSDate (Plugins)

- (NSString *)stringFromDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    [dateFormatter release];
    return destDateString;
}

- (NSString *)stringFromDateEx
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    [dateFormatter release];
    return destDateString;
}

- (NSString *)stringFromDateDB
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmssSSS"];
    NSString *destDateString = [dateFormatter stringFromDate:self];
    [dateFormatter release];
    return destDateString;
}


@end
